﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LevelEditor.Forms
{
    public partial class EditorOptionsDialog : Form
    {
        public Color LayerBackColor { get; set; }

        public Color LayerGridColor { get; set; }

        public bool LayerEnableGrid { get; set; }

        public decimal LayerGridThickness { get; set; }

        public Color PaletteBackColor { get; set; }

        public Color PaletteGridColor { get; set; }

        public bool PaletteEnableGrid { get; set; }

        public decimal PaletteGridThickness { get; set; }

        public EditorOptionsDialog()
        {
            InitializeComponent();
        }

        private Color DoColorDialog()
        {
            Color returnColor = Color.Empty;
            ColorDialog cd = new ColorDialog();
            DialogResult dr = cd.ShowDialog(this);
            if (dr == DialogResult.OK)
                returnColor =  cd.Color;
            cd.Dispose();
            return returnColor;
        }

        private void EditorOptionsDialog_Load(object sender, EventArgs e)
        {
            layerBackColorButton.BackColor = LayerBackColor;
            layerGridCB.Checked = LayerEnableGrid;
            layerGridThicknessNUD.Value = LayerGridThickness;
            layerGridColorButton.BackColor = LayerGridColor;
            paletteBackColorButton.BackColor = PaletteBackColor;
            paletteGridCB.Checked = PaletteEnableGrid;
            paletteGridThicknessNUD.Value = PaletteGridThickness;
            paletteGridColorButton.BackColor = PaletteGridColor;
        }

        private void okayButton_Click(object sender, EventArgs e)
        {
            LayerBackColor = layerBackColorButton.BackColor;
            LayerEnableGrid = layerGridCB.Checked;
            LayerGridThickness = layerGridThicknessNUD.Value;
            LayerGridColor = layerGridColorButton.BackColor;
            PaletteBackColor = paletteBackColorButton.BackColor;
            PaletteEnableGrid = paletteGridCB.Checked;
            PaletteGridThickness = paletteGridThicknessNUD.Value;
            PaletteGridColor = paletteGridColorButton.BackColor;
            DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void layerBackColorButton_Click(object sender, EventArgs e)
        {
            Color c = DoColorDialog();
            if (c != Color.Empty)
                layerBackColorButton.BackColor = c;
        }

        private void layerGridColorButton_Click(object sender, EventArgs e)
        {
            Color c = DoColorDialog();
            if (c != Color.Empty)
                layerGridColorButton.BackColor = c;
        }

        private void paletteBackColorButton_Click(object sender, EventArgs e)
        {
            Color c = DoColorDialog();
            if (c != Color.Empty)
                paletteBackColorButton.BackColor = c;
        }

        private void paletteGridColorButton_Click(object sender, EventArgs e)
        {
            Color c = DoColorDialog();
            if (c != Color.Empty)
                paletteGridColorButton.BackColor = c;
        }

        private void defaultsButton_Click(object sender, EventArgs e)
        {
            LayerBackColor = layerBackColorButton.BackColor = SystemColors.Control;
            LayerGridColor = layerGridColorButton.BackColor = SystemColors.ControlText;
            PaletteBackColor = paletteBackColorButton.BackColor = SystemColors.Control;
            PaletteGridColor = paletteGridColorButton.BackColor = SystemColors.ControlText;
            LayerEnableGrid = layerGridCB.Checked = true;
            PaletteEnableGrid = paletteGridCB.Checked = true;
            LayerGridThickness = layerGridThicknessNUD.Value = 1;
            PaletteGridThickness = paletteGridThicknessNUD.Value = 1;

        }
    }
}
